# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def error_msg(object, error, msg)
    return "<span class=\"errorExplan\">#{msg}</span>" if object && object.errors[error]
  end
  
  def avatar_for(user, size = "")
    user.avatar_url.blank? ? "<img src=\"/images/avatar_#{size}.png\" class=\"avatar\" alt=\"#{user.login}\" />" : image_tag(url_for_file_column(user, "avatar_url", size), :class => "avatar", :alt => user.login)
  end
  
  def areas_crumble_path_with_link(area)
    result = ""
    area.ancestors.each do |p| 
      result += yield(p) + " > "
    end
    result += area.title
  end
  
  def show_date(datetime)
    "#{datetime.year}-#{datetime.month}-#{datetime.day}"
  end
  
  def show_datetime(datetime, timezone_ajust=true)
    datetime += 13.hour if timezone_ajust
    wday = %w(星期日 星期一 星期二 星期三 星期四 星期五 星期六)
    show_date(datetime) + "&nbsp;#{wday[datetime.wday]}" + "&nbsp;#{sprintf('%.2d', datetime.hour)}:#{sprintf('%.2d',datetime.min)}"
  end
  
  def nav_menu(text, url, session_name)
    if session[:nav] == session_name
      return link_to(text, url, :class => "now")
    else
      return link_to(text, url)
    end
  end
  
  def inbox_title(unread_amount)
    if unread_amount == 0
      return "我的消息"
    else
      return "<strong>我的消息(#{unread_amount})</strong>"
    end
  end
  
  def url_for_area(area_id)
    url_for :controller => 'schools', :action => 'area', :id => area_id
  end
end
